package com.xinchao.ck_mips.utils;

import android.text.TextUtils;

import com.xinchao.ck_mips.constant.CK;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期处理类
 */
public class DateUtil {

    /**
     * 时间字符串转Calendar
     *
     * @param time
     * @return
     */
    public static Calendar stringToCalendar(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        try {
            Date date = sdf.parse(time);
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY, date.getHours());
            calendar.set(Calendar.MINUTE, date.getMinutes());
            return calendar;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 判断时间是否在时间段内
     *
     * @param now   当前时间
     * @param begin 开始时间
     * @param end   结束时间
     * @return 是否在时间段内
     */
    public static boolean belongCalendar(Calendar now, Calendar begin, Calendar end) {
        if (now.after(begin) && now.before(end)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断当前时间是不是周六、周日
     *
     * @return
     */
    public static boolean isWeekDay() {
        Calendar today = Calendar.getInstance();
        today.setTime(new Date());
        if (today.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || today.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断当前时间是不是工作日（周一到周五）
     *
     * @return
     */
    public static boolean isWorkDay() {
        Calendar workDay = Calendar.getInstance();
        workDay.setTime(new Date());
        if (workDay.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY && workDay.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 是否周六
     *
     * @return
     */
    public static boolean isSaturday() {
        Calendar workDay = Calendar.getInstance();
        workDay.setTime(new Date());
        if (workDay.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 是否周一到周四
     *
     * @return
     */
    public static boolean isMonDayToThursDay() {
        Calendar workDay = Calendar.getInstance();
        workDay.setTime(new Date());
        if (workDay.get(Calendar.DAY_OF_WEEK) == Calendar.TUESDAY || workDay.get(Calendar.DAY_OF_WEEK) == Calendar.WEDNESDAY
                || workDay.get(Calendar.DAY_OF_WEEK) == Calendar.THURSDAY || workDay.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 是否周日
     *
     * @return
     */
    public static boolean isSunday() {
        Calendar workDay = Calendar.getInstance();
        workDay.setTime(new Date());
        if (workDay.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 是否周五
     *
     * @return
     */
    public static boolean isFriday() {
        Calendar workDay = Calendar.getInstance();
        workDay.setTime(new Date());
        if (workDay.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取今天是周几
     *
     * @return
     */
    public static String getDayOfWeek() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        //与后台返回请求值对应0~6
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        if (dayOfWeek == Calendar.MONDAY) {
            return "0";
        } else if (dayOfWeek == Calendar.TUESDAY) {
            return "1";
        } else if (dayOfWeek == Calendar.WEDNESDAY) {
            return "2";
        } else if (dayOfWeek == Calendar.THURSDAY) {
            return "3";
        } else if (dayOfWeek == Calendar.FRIDAY) {
            return "4";
        } else if (dayOfWeek == Calendar.SATURDAY) {
            return "5";
        } else {
            return "6";
        }

    }

    /**
     * 是否周一
     *
     * @return
     */
    public static boolean isMonday() {
        Calendar workDay = Calendar.getInstance();
        workDay.setTime(new Date());
        if (workDay.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
            return true;
        } else {
            return false;
        }
    }

    public static String[] getWorkDayOnTime() {
        String workDayOnTime = ShareUtil.getInstance().getString(CK.WORK_DAY_ON_TIME, "");
        String[] onStr = new String[]{};
        if (!TextUtils.isEmpty(workDayOnTime)) {
            onStr = workDayOnTime.split(":");
        }

        return onStr;
    }

    public static String[] getWorkDayOffTime() {
        String workDayOffTime = ShareUtil.getInstance().getString(CK.WORK_DAY_OFF_TIME, "");
        String[] offStr = new String[]{};
        if (!TextUtils.isEmpty(workDayOffTime)) {
            offStr = workDayOffTime.split(":");
        }

        return offStr;
    }

    public static String[] getWeekDayOnTime() {
        String weekDayOnTime = ShareUtil.getInstance().getString(CK.WEEK_DAY_ON_TIME, "");
        String[] onStr = new String[]{};
        if (!TextUtils.isEmpty(weekDayOnTime)) {
            onStr = weekDayOnTime.split(":");
        }

        return onStr;
    }

    public static String[] getWeekDayOffTime() {
        String weekDayOffTime = ShareUtil.getInstance().getString(CK.WEEK_DAY_OFF_TIME, "");
        String[] offStr = new String[]{};
        if (!TextUtils.isEmpty(weekDayOffTime)) {
            offStr = weekDayOffTime.split(":");
        }

        return offStr;
    }

    /**
     * 获取当前日期小时
     *
     * @return
     */
    public static int getNowHour() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    /**
     * 获取当前日期分钟
     *
     * @return
     */
    public static int getNowMinute() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        return calendar.get(Calendar.MINUTE);
    }
}
